import 'dart:convert';

import 'package:core_package/core_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';

class ProtocolPage extends StatefulWidget {
  final int? isProtocol;
  static const String routeName = '/protocolPage';

  const ProtocolPage({Key? key, this.isProtocol}) : super(key: key);
  @override
  ProtocolPageState createState() {
    return ProtocolPageState();
  }
}

class ProtocolPageState extends State<ProtocolPage> {
  WebViewController? _controller;

  @override
  Widget build(BuildContext context) {
    String title;
    if(widget.isProtocol==0){
      title="《净水设备服务隐私政策》";

    }else if(widget.isProtocol ==1){
      title = "《注册协议》";

    }else{
      title ='推广说明';
    }
    return Scaffold(
      appBar: MyAppBar(context,title:title),
      body: WebView(
        initialUrl:widget.isProtocol==0?'https://51uuid.com/tgh5/yinsi.htm': 'about:blank',
        onWebViewCreated: (WebViewController webViewController) {
          _controller = webViewController;
          if(widget.isProtocol!=0)
          _loadHtmlFromAssets();
        },
      ),
    );
  }

  _loadHtmlFromAssets() async {
    String title;
    if(widget.isProtocol==0){
      title = 'assets/file/净水设备服务隐私政策.html';
    }else if(widget.isProtocol ==1){
      title= "assets/file/注册协议.html";
    }else{
      title ="assets/file/content.html";
    }
    String fileText = await rootBundle.loadString(title);
    _controller?.loadUrl( Uri.dataFromString(
        fileText,
        mimeType: 'text/html',
        encoding: Encoding.getByName('utf-8')
    ).toString());
  }
}
